<div class="pager-main">
    <div class="pagination-box">

        {if $pager['pageNum'] > 1}
        <div class="pagination pager">

            {if $pager['page'] == 1}
                <span>首页</span>
                <span>上一页</span>
            {else}
                <a href="{:searchUrl('@store/apps',['page' => 1])}">首页</a>
                <a href="{:searchUrl('@store/apps',['page' => $pager['page'] - 1])}">上一页</a>
            {/if}

            {for start="1" end="$pager['pageNum'] + 1"}

            {if $pager['page'] <= 5}
            {if $i <= 6}
                <a href="{:searchUrl('@store/apps',['page' => $i])}" class="{if $i == $pager['page']}active{/if} border">{$i}</a>
            {/if}
            {else}
            {if $pager['page'] - 3 < $i && $i <= 2 + $pager['page'] }
                <a href="{:searchUrl('@store/apps',['page' => $i])}" class="{if $i == $pager['page']}active{/if} border">{$i}</a>
            {/if}
            {/if}

            {/for}

            {if $pager['page'] == $pager['pageNum']}
                <span>下一页</span>
                <span>尾页</span>
            {else}
                <a href="{:searchUrl('@store/apps',['page' => $pager['page'] + 1])}">下一页</a>
                <a href="{:searchUrl('@store/apps',['page' => $pager['pageNum']])}">尾页</a>
            {/if}
        </div>

        <script>
            require(["jquery"], () => {
                $(".pager a").on('click', function () {
                    let page = $(this).data('page');

                    let params = getRequest(page);
                    let paramsStr = Object.keys(params).map(function (key) {
                        return encodeURIComponent(key) + "=" + encodeURIComponent(params[key]);
                    }).join("&");

                    location.href = getCaption(location.href, 0) + '?' + paramsStr;
                });

                function getRequest(page = 1) {
                    let url = location.search;
                    let str = url.substr(1);

                    let theRequest = [];
                    if (url.indexOf("?") !== -1) {
                        let strs = str.split("&");
                        for (let i = 0; i < strs.length; i++) {
                            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                        }
                    }

                    theRequest.page = page;
                    // console.log(theRequest)
                    return theRequest;
                }

                function getCaption(obj, state) {
                    let index = obj.lastIndexOf("\?");
                    if (state === 0) {
                        obj = obj.substring(0, index);
                    } else {
                        obj = obj.substring(index + 1, obj.length);
                    }
                    return obj;
                }
            });
        </script>
        {/if}

    </div>
</div>